// apiClient.js
const baseURL = 'https://eduwaker.top/smartfarm/farmapp/'
// const baseURL = 'http://localhost:8000/farmapp/' 
const apiClient = {
  interceptors: {
    request: null,
    response: null
  },

  setRequestInterceptor(callback) {
    this.interceptors.request = callback;
  },

  setResponseInterceptor(callback) {
    this.interceptors.response = callback;
  },

  get(url) {
    return new Promise((resolve, reject) => {
      const requestParams = {
        url,
        method: 'GET',
        success(res) {
          resolve(res);
        },
        fail(error) {
          reject(error);
        }
      };

      if (this.interceptors.request) {
        this.interceptors.request(requestParams);
      }

      wx.request(requestParams);
    });
  },
  post(url, data) {
    return new Promise((resolve, reject) => {
      const requestParams = {
        url,
        method: 'POST',
        data,
        success(res) {
          resolve(res);
        },
        fail(error) {
          reject(error);
        }
      };

      if (this.interceptors.request) {
        this.interceptors.request(requestParams);
      }

      wx.request(requestParams);
    });
  },
  patch(url, data) {
    return new Promise((resolve, reject) => {
      const requestParams = {
        url,
        method: 'PATCH',
        data,
        success(res) {
          resolve(res);
        },
        fail(error) {
          reject(error);
        }
      };

      if (this.interceptors.request) {
        this.interceptors.request(requestParams);
      }

      wx.request(requestParams);
    });
  },
  put(url, data) {
    return new Promise((resolve, reject) => {
      const requestParams = {
        url,
        method: 'PUT',
        data,
        success(res) {
          resolve(res);
        },
        fail(error) {
          reject(error);
        }
      };

      if (this.interceptors.request) {
        this.interceptors.request(requestParams);
      }

      wx.request(requestParams);
    });
  },
  delete(url,id) {
    const URL = url+id+'/'
    return new Promise((resolve, reject) => {
      const requestParams = {
        url,
        method: 'DELETE',
        success(res) {
          resolve(res);
        },
        fail(error) {
          reject(error);
        }
      };

      if (this.interceptors.request) {
        this.interceptors.request(requestParams);
      }

      wx.request(requestParams);
    });
  }
};

// 设置请求拦截器
apiClient.setRequestInterceptor((requestParams) => {
  // 从微信小程序的storage中获取accessToken
  const accessToken = wx.getStorageSync('accessToken');
  if (accessToken) {
    requestParams.header = {
      ...(requestParams.header || {}),
      'Authorization': `Token ${accessToken}`
    };
  }
});

// 路由管理
const ROUTES = {
  base:'',
  login: 'login/',
  logout: 'logout/',
  register: 'register/',
  group_properties:'group_properties/',
  users: 'users/',
  switches: 'switches/',
  devices: 'devices/',
  cameras: 'cameras/',
  device_data:'device_data/',
  latest_data: 'latest_data/',
  device_latest: 'device_latest/',
  latest_data: 'latest_data/',
  orgs: 'orgs/',
  hls:'hls/'
}

// 使用map和其他方法为每个路由值添加baseURL
const apiRoutes = Object.fromEntries(
  Object.entries(ROUTES).map(([key, value]) => [key, baseURL + value])
)


export {apiClient, apiRoutes};